﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Vote.Database.DataAccess;
using Vote.DataEntity;

namespace Vote.Business
{
    public class VoteModel
    {
        #region 单例
        private VoteModel() { }
        private static VoteModel _Instance = new VoteModel();
        public static VoteModel Instance { get { return _Instance; } }
        #endregion

        #region Public Mothodes


        // 获取投票列表
        public ActionEntity GetVoteActionByActionID(int actionId)
        {
            var voteItemList = new ActionAccess().GetVoteItemList(actionId);
            var voteGroup = voteItemList.GroupBy(O => O.GroupID).ToList();
            var firstItem = voteItemList.FirstOrDefault();

            var voteAction = new ActionEntity();
            if (firstItem != null)
            {
                voteAction = new ActionEntity()
                {
                    ActionID = actionId,
                    ActionName = firstItem.ActionName,
                    ActionDesc = firstItem.ActionDesc,
                    BeginTime = firstItem.Begintime,
                    EndTime = firstItem.Endtime,
                    VoteList = new List<GroupEntity>()
                };
                foreach (var vote in voteGroup)
                {
                    var firstGroup = vote.FirstOrDefault();
                    var voteModel = new GroupEntity()
                    {
                        GroupID = vote.Key,
                        GroupName = firstGroup.GroupName,
                        GroupDesc = firstGroup.GroupDesc,
                        MinSelectedCount = firstGroup.MinNum,
                        MaxSelectedCount = firstGroup.MaxNum,
                        VoteType = firstGroup.VoteType,
                        VoteItemList = new List<VoteItemEntity>(),
                    };
                    voteModel.VoteItemList = vote.ToList().ConvertAll(O => new VoteItemEntity()
                    {
                        ItemID = O.ItemID,
                        ItemName = O.ItemName,
                        ImageUrl = O.ImageUrl,
                        RelationVideo = O.RelationVideo,
                        Desc = O.Desc,
                    });

                    voteAction.VoteList.Add(voteModel);
                }
            }

            return voteAction;
        }

        #region 后台管理
        /// <summary>
        /// 获取活动列表
        /// </summary>
        /// <returns></returns>
        public List<ActionEntity> GetActionList(Int32 index, Int32 count)
        {
            var actionDataList = new ActionAccess().GetActionList(index, count);
            var actionInfo = actionDataList.ConvertAll(A => new ActionEntity() { ActionID = A.ID, ActionName = A.ActionName, CreatTime = A.CreateTime, BeginTime = A.BeginTime, EndTime = A.EndTime, VoteCount = A.VoteCount });

            return actionInfo;
        }


        /// <summary>
        /// 获取投票统计结果
        /// </summary>
        /// <returns></returns>
        public ActionEntity GetVoteResultByActionID(int actionId)
        {
            var voteResult = new ResultAccess().GetVoteResult(actionId);
            var voteResultGroup = voteResult.GroupBy(V => V.GroupID);
            var firstVoteResult = voteResult.FirstOrDefault();

            var voteAction = new ActionEntity();
            if (firstVoteResult != null)
            {
                voteAction = new ActionEntity() { ActionID = firstVoteResult.ActionID, ActionName = firstVoteResult.Actionname, BeginTime = firstVoteResult.BeginTime, EndTime = firstVoteResult.EndTime, VoteList = new List<GroupEntity>(), };
                foreach (var vo in voteResultGroup)
                {
                    var group = new GroupEntity() { GroupID = vo.Key, GroupName = vo.FirstOrDefault().GroupName, VoteItemList = new List<VoteItemEntity>(), };
                    group.VoteItemList = vo.ToList().ConvertAll(O => new VoteItemEntity() { ItemID = O.ItemID, ItemName = O.ItemName, VoteCount = O.VoteCount, RelationVideo = string.Empty, });

                    voteAction.VoteList.Add(group);
                }
            }

            return voteAction;
        }

        /// <summary>
        /// 获取投票统计结果
        /// </summary>
        /// <returns></returns>
        public List<ActionEntity> GetVoteResult(string actionlist)
        {
            var voteResult = new ResultAccess().GetVoteResult(actionlist);

            var list = new List<ActionEntity>() { };
            var actionGroupList = voteResult.GroupBy(A => A.ActionID);
            foreach (var actionGroup in actionGroupList)
            {
                var voteResultGroup = actionGroup.GroupBy(V => V.GroupID);
                var firstVoteResult = actionGroup.FirstOrDefault();

                var voteAction = new ActionEntity();
                if (firstVoteResult != null)
                {
                    voteAction = new ActionEntity() { ActionID = firstVoteResult.ActionID, ActionName = firstVoteResult.Actionname, BeginTime = firstVoteResult.BeginTime, EndTime = firstVoteResult.EndTime, VoteList = new List<GroupEntity>(), };
                    foreach (var vo in voteResultGroup)
                    {
                        var group = new GroupEntity() { GroupID = vo.Key, GroupName = vo.FirstOrDefault().GroupName, VoteItemList = new List<VoteItemEntity>(), };
                        group.VoteItemList = vo.ToList().ConvertAll(O => new VoteItemEntity() { ItemID = O.ItemID, ItemName = O.ItemName, VoteCount = O.VoteCount, RelationVideo = string.Empty, });

                        voteAction.VoteList.Add(group);
                    }
                }

                list.Add(voteAction);
            }

            return list;
        }

        /// <summary>
        /// 返回选择某个选项的用户列表
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public List<string> GetUserIDListByItemID(int itemId)
        {
            var userList = new ResultAccess().GetUserIDListByItemID(itemId);

            return userList;
        }

        /// <summary>
        /// 根据ActionID获取用户列表
        /// </summary>
        /// <param name="voterLists"></param>
        /// <returns></returns>
        public List<VoterDataEntity> GetVotersByActionID(int actionId)
        {
            return new ResultAccess().GetVotersByActionID(actionId);
        }

        /// <summary>
        /// 后台保存投票活动，分组，选项
        /// </summary>
        /// <returns></returns>
        public Int32 SaveActionDetails(ActionDataEntity entity)
        {
            Int32 result = new UserActionAccess().SaveActionVoteDetail(entity);

            return result;
        }

        /// <summary>
        /// 删除活动
        /// </summary>
        /// <param name="id">活动主键Id</param>
        /// <returns></returns>
        public Boolean DeleteActionDetails(Int32 id)
        {
            var imglist = new ActionAccess().GetImageUrlList(id);
            // 获取要删除图片列表
            foreach (var img in imglist)
            {
                try
                {
                    string serverPath = System.Web.HttpContext.Current.Server.MapPath("~");
                    string toFilePath = Path.Combine(serverPath, @"upload\");
                    //获得要保存的文件路径
                    string serverFileName = toFilePath + img;

                    File.Delete(serverFileName);
                }
                catch (Exception ex)
                { }
            }
            // 删除活动
            Boolean result = new UserActionAccess().DeleteActionDetails(id);

            return result;
        }
        #endregion

        #endregion

    }

    #region DTO

    public class ActionEntity
    {
        /// <summary>
        /// 投票活动ID
        /// </summary>
        public int ActionID { get; set; }

        public string ActionName { get; set; }
        public string ActionDesc { get; set; }

        public DateTime CreatTime { get; set; }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime BeginTime { get; set; }
        public DateTime EndTime { get; set; }

        public List<GroupEntity> VoteList { get; set; }
        /// <summary>
        /// 参与投票的人数，如果大于0不能编辑删除
        /// </summary>
        public Int32 VoteCount { get; set; }
    }

    public class GroupEntity
    {
        public int GroupID { get; set; }

        public string GroupName { get; set; }
        public string GroupDesc { get; set; }
        public int MinSelectedCount { get; set; }
        public int MaxSelectedCount { get; set; }

        public int VoteType { get; set; }

        public List<VoteItemEntity> VoteItemList { get; set; }

    }

    public class VoteItemEntity
    {
        public int ItemID { get; set; }
        public string ItemName { get; set; }
        public string ImageUrl { get; set; }
        public string RelationVideo { get; set; }
        public string Desc { get; set; }
        /// <summary>
        /// 得票数
        /// </summary>
        public int VoteCount { get; set; }
    }

    #endregion
}
